package com.cloud.wms.ui.inside.replenish;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.widget.ScanText;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.inside.contract.LackQueryContract;
import com.cloud.wms.ui.inside.entity.LackCommodityEntity;
import com.cloud.wms.ui.inside.presenter.LackQueryPresenter;
import com.cloud.wms.ui.main.entity.CommodityEntity;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.widget.dialog.AlertDialogBuilder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @Author:Chen
 * @Date: 2021-01-26 15:28
 * @Description:
 */
public class LackQueryActivity extends BaseBarListActivity<LackCommodityEntity, LackQueryPresenter> implements LackQueryContract.View {
    @BindView(R.id.iv_search)
    ImageButton ivSearch;
    @BindView(R.id.scan_text)
    ScanText scanText;
    @BindView(R.id.btn_select_all)
    TextView btnSelectAll;
    @BindView(R.id.btn_create_replenish)
    TextView btnCreateReplenish;
    private String taskCode;

    @Override
    protected String barTitle() {
        return "缺货查询";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_inside_lack_query;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_inside_lack_commodity;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        taskCode = getIntent().getStringExtra(IntentKey.INFO_KEY);
        scanText.setHint("商品条码");
        initScanText(scanText);
        mPresenter.loadLackCommodity("", page = 0);
    }

    @Override
    public void onReload() {
        super.onReload();
        mPresenter.loadLackCommodity("", page = 0);
    }

    @Override
    protected boolean isRefresh() {
        return true;
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }

    @Override
    protected void loadListData(int page) {
        super.loadListData(page);
        mPresenter.loadLackCommodity(scanText.getText(), page);
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        scanText.addHistory(result);
        mPresenter.loadLackCommodity(result, page = 0);
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, LackCommodityEntity item) {
        helper.setText(R.id.tv_commodity_code, item.getBarCode())
                .setText(R.id.tv_commodity_name, item.getCommodityName())
                .setText(R.id.tv_stock_type, item.getStockTypeName())
                .setText(R.id.tv_batch_code, item.getProductBatchCode())
                .setText(R.id.tv_pos_area, item.getDestAreaName())
                .setText(R.id.tv_pos_code, item.getDestPosCode())
                .setText(R.id.tv_commodity_amount, item.getPlanAmount() + item.getLackAmount() + "");

        CheckBox checkBox = (CheckBox) helper.getView(R.id.cb_select_state);
        checkBox.setChecked(item.isFlag());
        checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            item.setFlag(isChecked);
            for (int i = 0; i < mAdapter.getData().size(); i++) {
                if (mAdapter.getData().get(i).isFlag()) {
                    btnCreateReplenish.setEnabled(true);
                    return;
                }
            }
            btnCreateReplenish.setEnabled(false);
        });
    }

    @Override
    protected void itemClickListener(BaseQuickAdapter<LackCommodityEntity, BaseViewHolder> mAdapter, View view, int position) {
        super.itemClickListener(mAdapter, view, position);
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
        switch (message.what) {
            case LackQueryPresenter.CREATE_REPLENISH_ORDER:
                page = 0;
                onAlert("补货单创建成功，是否立即执行当前补货单？",
                        (dialog, which) -> mPresenter.receiveTask(((long) message.obj), taskCode));
                break;
            case LackQueryPresenter.TASK:
                TaskEntity taskEntity = (TaskEntity) message.obj;
                ReplenishDownBindActivity.start(this, taskEntity);
                break;
        }
    }

    @OnClick({R.id.iv_search, R.id.btn_select_all, R.id.btn_create_replenish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_search:
                String text = scanText.getText();
                mPresenter.loadLackCommodity(text, page = 0);
                break;
            case R.id.btn_select_all:
                for (int i = 0; i < mAdapter.getData().size(); i++) {
                    mAdapter.getData().get(i).setFlag(btnSelectAll.getText().equals("全选"));
                }
                mAdapter.notifyDataSetChanged();
                btnCreateReplenish.setEnabled(true);
                btnSelectAll.setText(btnSelectAll.getText().equals("全选") ? "全不选" : "全选");
                break;
            case R.id.btn_create_replenish:
                ArrayList<LackCommodityEntity> entities = new ArrayList<>();
                for (int i = 0; i < mAdapter.getData().size(); i++) {
                    if (mAdapter.getData().get(i).isFlag()) {
                        entities.add(mAdapter.getData().get(i));
                    }
                }
                if (entities.isEmpty()) {
                    info("请选择缺货商品！");
                    return;
                }
                mPresenter.createReplenishOrder(entities);
                break;
        }
    }
}
